package cn.stylefeng.guns.modular.vlt.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.vlt.entity.Cookie;
import cn.stylefeng.guns.modular.vlt.mapper.CookieMapper;
import cn.stylefeng.guns.modular.vlt.model.params.CookieParam;
import cn.stylefeng.guns.modular.vlt.model.result.CookieResult;
import  cn.stylefeng.guns.modular.vlt.service.CookieService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 爬虫设置 服务实现类
 * </p>
 *
 * @author 
 * @since 2021-01-20
 */
@Service
public class CookieServiceImpl extends ServiceImpl<CookieMapper, Cookie> implements CookieService {

    @Override
    public void add(CookieParam param){
        Cookie entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CookieParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CookieParam param){
        Cookie oldEntity = getOldEntity(param);
        Cookie newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setId(oldEntity.getId());
        this.updateById(newEntity);
    }

    @Override
    public CookieResult findBySpec(CookieParam param){
        return null;
    }

    @Override
    public List<CookieResult> findListBySpec(CookieParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CookieParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CookieParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Cookie getOldEntity(CookieParam param) {
        //return this.getById(getKey(param));
        return this.getOne(null);
    }

    private Cookie getEntity(CookieParam param) {
        Cookie entity = new Cookie();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
